package cn.funeralobjects.asms.service.impl;

import cn.funeralobjects.asms.entity.ESmsTemplate;
import cn.funeralobjects.asms.model.BasicInfo;
import cn.funeralobjects.asms.model.SmsTemplate;
import cn.funeralobjects.asms.repository.ESmsTemplateRepository;
import cn.funeralobjects.asms.service.SmsTemplateService;
import cn.funeralobjects.common.jpa.service.JpaCodeService;
import cn.funeralobjects.common.jpa.service.JpaEntityService;
import cn.funeralobjects.common.jpa.service.JpaModifyService;
import cn.funeralobjects.common.jpa.service.JpaNameService;
import cn.funeralobjects.common.repository.CodeRepository;
import cn.funeralobjects.common.repository.EntityRepository;
import cn.funeralobjects.common.repository.NameRepository;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;
import org.springframework.data.repository.CrudRepository;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/5/31 10:37 PM
 */
@CommonService
public class SmsTemplateServiceImpl implements SmsTemplateService, JpaModifyService<SmsTemplate, ESmsTemplate, Integer>, JpaEntityService<ESmsTemplate, Integer>, JpaCodeService<ESmsTemplate, Integer>, JpaNameService<ESmsTemplate, Integer> {

    @Resource
    private ESmsTemplateRepository eSmsTemplateRepository;


    @Override
    public void checkConflict(SmsTemplate smsTemplate) throws DataConflictException {
        this.mustNotConflictByCode(smsTemplate.getTemplateCode());
    }

    @Override
    public ESmsTemplate toEntity(SmsTemplate smsTemplate) {
        return smsTemplate == null ? null : new ESmsTemplate()
                .setCreateDate(smsTemplate.getCreateDate())
                .setProdResCode(smsTemplate.getProdResCode())
                .setReason(smsTemplate.getReason())
                .setStatus(smsTemplate.getStatus())
                .setTemplateCode(smsTemplate.getTemplateCode())
                .setTemplateContent(smsTemplate.getTemplateContent())
                .setTemplateName(smsTemplate.getTemplateName())
                .setType(smsTemplate.getType());
    }

    @Override
    public CodeRepository<ESmsTemplate, Integer> codeRepository() {
        return eSmsTemplateRepository;
    }


    @Override
    public CrudRepository<ESmsTemplate, Integer> modifyRepository() {
        return eSmsTemplateRepository;
    }

    @Override
    public NameRepository<ESmsTemplate, Integer> nameRepository() {
        return eSmsTemplateRepository;
    }

    @Override
    public void checkConflict(SmsTemplate smsTemplate, Integer withoutId) throws DataConflictException {
        this.mustNotConflictByCode(smsTemplate.getTemplateCode(), withoutId);
    }

    @Override
    public void copyToEntity(SmsTemplate sourceModify, ESmsTemplate targetEntity) {
        targetEntity.setCreateDate(sourceModify.getCreateDate())
                .setProdResCode(sourceModify.getProdResCode())
                .setReason(sourceModify.getReason())
                .setStatus(sourceModify.getStatus())
                .setTemplateCode(sourceModify.getTemplateCode())
                .setTemplateContent(sourceModify.getTemplateContent())
                .setTemplateName(sourceModify.getTemplateName())
                .setType(sourceModify.getType());
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(SmsTemplate smsTemplate) {
        Assert.argAssert(smsTemplate, "smsTemplate");
    }

    @Override
    public String getName() {
        return ENTITY_NAME;
    }

    @Override
    public String getModuleName() {
        return BasicInfo.MODULE_NAME;
    }

    @Override
    public EntityRepository<ESmsTemplate, Integer> entityRepository() {
        return eSmsTemplateRepository;
    }
}
